package com.gqz.patrol.app.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
 * @Author: gqz1
 */
public class GenTaskNoTool {
    public static String createTaskNo() {
        // 当前时间字符串-到毫秒
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String now = LocalDateTime.now().format(formatter);
        // 随机数
        Random rand = new Random();
        String num4Str = String.format("%04d", rand.nextInt(1000));
        return now + num4Str;
    }

    /**
     * 创建递增工单ID
     *
     * @return
     */
    public static String createIncrementTaskNo(String taskNo) {
        //添加三位递增ID
        if (taskNo == null) {
            return null;
        }
        if (taskNo.length() == 21) {
            String num = NumberUtil.decimalFormat("000", 1);
            return taskNo + num;
        }
        int num = Integer.parseInt(StrUtil.subWithLength(taskNo, 21, 3));
        return StrUtil.replace(taskNo, 21, 24, NumberUtil.decimalFormat("000", num + 1));
    }

    public static void main(String[] args) throws JsonProcessingException {
        String s = IdUtil.getSnowflakeNextIdStr();
        System.out.println(s);
    }

}
